package com.jyhs.maven.servlet;

import com.alibaba.fastjson.JSON;
import com.jyhs.maven.dao.factory.*;
import com.jyhs.maven.proxy.dynamic.CglibProxyFactory;
import com.jyhs.maven.proxy.dynamic.JDKProxyFactory;
import com.jyhs.maven.service.TransferService;
import com.jyhs.maven.util.ReflectUtil;
import com.jyhs.maven.entity.Transfer;
import com.jyhs.maven.entity.dto.LayuiPage;
import com.jyhs.maven.entity.dto.TransferRecord;
import com.jyhs.maven.entity.dto.TransferRecordQuery;
import com.jyhs.maven.service.impl.TransferServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

@WebServlet(urlPatterns = "/tansfer")
public class TransferServlet extends HttpServlet {



//    TransferService transferService = JDKProxyFactory.create(new TransferServiceImpl());
TransferServiceImpl transferService = CglibProxyFactory.createProxy(new TransferServiceImpl());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = req.getParameter("opr");
        try {
            if ("add".equals(opr)){
                add(req,resp);
            }else if("getList".equals(opr)){
                getList(req,resp);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     *  添加转账记录
     * @param req
     * @param resp
     */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ClassNotFoundException, IOException, ParseException, IllegalAccessException {

        Transfer transfer = new Transfer();
        ReflectUtil.httpServletRequestToObject(req,transfer);
//        transfer.setPayUserId(Long.valueOf(req.getParameter("payUserId")));
//        transfer.setReciveUserId(Long.valueOf(req.getParameter("reciveUserId")));
//        transfer.setBalance(new BigDecimal(req.getParameter("balance")));

        PrintWriter out = resp.getWriter();
        AbstractTransferDaoFactory transferDaoFactory = new TansferDaoMabatisImplFactory();
        transferService.setTransferDao(transferDaoFactory.getInstance());


//        transferService.setTransferDao(TransferDaoFactory.getInstance("jdbc"));


        out.write(transferService.saveTransfer(transfer)==1?"转账成功":"余额不足");
        out.flush();
        out.close();
    }

    /**
     * 获取转账记录列表
     * @param req
     * @param resp
     */
    public void getList(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ClassNotFoundException, IOException, ParseException, IllegalAccessException {

//        List<TransferRecord> transferList = transferService.getTransferList(Integer.valueOf(req.getParameter("page")), Integer.valueOf(req.getParameter("limit")));
        TransferRecordQuery transferRecordQuery = new TransferRecordQuery();
        ReflectUtil.httpServletRequestToObject(req,transferRecordQuery);

//        AbstractTransferDaoFactory transferDaoFactory = new TransferDaoJdbcFactory();
//
//        transferService.setTransferDao(transferDaoFactory.getInstance());
//静态代理
//        AbstractTransferDaoFactory transferDaoFactory = new TransferDaoMybatisplusFactory();
//        transferService.setTransferDao(transferDaoFactory.getInstance());

//        transferService.setTransferDao(JDKProxyFactory.create(new TransferDaoImpl()));
        List<TransferRecord> transferList = transferService.getTransferList(transferRecordQuery);

        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");

        PrintWriter out = resp.getWriter();

        int count = transferService.getTransferCount(transferRecordQuery);
        LayuiPage layuiPage = new LayuiPage(transferList,count);

        out.write(JSON.toJSONString(layuiPage));
        out.flush();
        out.close();
    }





    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
